﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Red_Cross_Philippines;

namespace Philippine_National_Red_Cross
{
    public partial class frmSignup : Form
    {
        private readonly Random random = new Random();
        Account_Registration reg = new Account_Registration();
        Blood_Bank_System BloodSystem = new Blood_Bank_System();
        DataTable tbl = new DataTable();

        private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789";
        public frmSignup()
        {
            InitializeComponent();
            btnRegister.Click += new EventHandler(btnRegister_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
            this.Load += new EventHandler(Form1_Load);
            txtEmail.TextChanged += new EventHandler(txtEmail_TextChanged);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }

       void txtEmail_TextChanged(object sender, EventArgs e)
        {
            txtEmail.ForeColor = Color.Black;
        }

   
        void btnRegister_Click(object sender, EventArgs e)
        {

            if (txtFirst.Text == "")
            {
                MessageBox.Show("Please input your firstname", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtFirst.Focus();
                return;
            }

            else if (txtLast.Text == "")
            {
                MessageBox.Show("Please input your last name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtLast.Focus();
                return;
            }

            else if (txtEmail.Text == "")
            {
                MessageBox.Show("Please input your email address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtEmail.Focus();
                return;
            }

            else if (isEmail(txtEmail.Text) == false)
            {
                MessageBox.Show("Invalid email address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtEmail.ForeColor = Color.Red;
                txtEmail.Focus();
                return;
            }

            else if (txtUsername.Text == "")
            {
                MessageBox.Show("Please input your username", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtUsername.Focus();
                return;
            }
            else if (txtPassword.Text == "")
            {
                MessageBox.Show("Please input your password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtPassword.Focus();
                return;
            }
            else if (txtConfirm.Text == "")
            {
                MessageBox.Show("Please confirm your password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtConfirm.Focus();
                return;
            }

            else if (txtConfirm.Text != txtPassword.Text)
            {
                MessageBox.Show("Password not match", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtConfirm.Focus();
                return;
            }



            else if (txtScText.Text != scramble_txt.Text)
            {
                MessageBox.Show("Incorrect", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                scramble_txt.Text = RandomString(8);
                return;
            }


            RegisterThis();
            Reset();
            MessageBox.Show("You have successfully registered your account", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            
        }
        void Form1_Load(object sender, EventArgs e)
        {
            scramble_txt.Text = RandomString(8);
            BloodSystem.Connect();
            BloodSystem.Query("SELECT * FROM tblusers");
            tbl = BloodSystem.tbl;
            BloodSystem.Disconnect();
        }

      

        public string RandomString(int size)
        {
            char[] chr = new char[size];
            for (int i = 0; i < size; i++)
            {
                chr[i] = chars[random.Next(chars.Length)];
            }
            return new string(chr);
        }

        public bool isEmail(string emailAdd)
        {
            int atPosition = emailAdd.IndexOf('@');
            if (atPosition > 0 && emailAdd.IndexOf('.', atPosition) > -1) return true;
            return false;
        }


        private void RegisterThis()
        {
            string adminName = txtFirst.Text + " " + txtLast.Text;
            reg.name = adminName;
            reg.email = txtEmail.Text;
            reg.username = txtUsername.Text;
            reg.password = txtPassword.Text;

            if (rdClient.Checked == true)
            {
                reg.position = "Client";
            }
            else
            {
                reg.position = "Admin";
            }
            BloodSystem.Register_Accnt(reg);
        }

        private void Reset()
        {
            txtFirst.Clear();
            txtLast.Clear();
            txtEmail.Clear();
            txtUsername.Clear();
            txtPassword.Clear();
            txtConfirm.Clear();
            txtScText.Clear();
        }

       


    }
}
